package eeui.android.eeuiIdentify.module;

import com.alibaba.fastjson.JSONObject;
import com.taobao.weex.annotation.JSMethod;
import com.taobao.weex.bridge.JSCallback;

import app.eeui.framework.extend.base.WXModuleBase;
import eeui.android.eeuiIdentify.module.biometric.BiometricCallback;
import eeui.android.eeuiIdentify.module.biometric.BiometricManager;

public class eeuiIdentifyAppModule extends WXModuleBase {

    private BiometricManager mBiometricManager;

    /**
     * 检查是否支持生物认证
     * @return
     */
    @JSMethod(uiThread = false)
    public boolean check() {
        mBiometricManager = BiometricManager.init(getActivity());
        return mBiometricManager.isCanAuthenticate();
    }

    /**
     * 开始生物认证
     * @param callback
     */
    @JSMethod
    public void start(JSCallback callback) {
        if (callback == null) {
            return;
        }
        JSONObject data = new JSONObject();
        if (!check()) {
            data.put("status", "error");
            data.put("msg", "不支持身份验证");
            callback.invoke(data);
            return;
        }
        BiometricManager.Builder builder = new BiometricManager.Builder().setTitle("验证身份");
        mBiometricManager.showBiometricPrompt(getActivity(), builder, new BiometricCallback() {
            @Override
            public void onSucceeded() {
                data.put("status", "success");
                data.put("msg", "验证成功");
                callback.invoke(data);
            }

            @Override
            public void onError(String errString) {
                data.put("status", "error");
                data.put("msg", errString);
                callback.invoke(data);
            }

            @Override
            public void onFailed() {
                super.onFailed();
                data.put("status", "error");
                data.put("msg", "验证失败");
                callback.invoke(data);
            }
        });
    }
}
